import os
import sys
localDir = os.path.dirname(__file__)
curpath = os.path.normpath(os.path.join(os.getcwd(), localDir))
grandparent = os.path.normpath(os.path.join(curpath, '../'))
sys.path.insert(0, grandparent)

import unittest

from lib.remove_index import RemoveIndex

from databases.mysql import Mysql
from databases.sqlite import Sqlite
from databases.db2 import Db2
from databases.mssql import Mssql
from databases.oracle import Oracle
from databases.postgres import Postgres

config = {"development" : {"db" : "sqlite:///:memory:"}}

class TestRemoveIndex(unittest.TestCase):

    def testTruth(self):
        self.assertTrue(True)

    def testInitialiazeMysql(self):
        alc = Mysql(config['development']['db'])

        query = "schema_info, schema_type"
        sql = RemoveIndex(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_type", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info DROP INDEX schema_type;", sql.query)

    def testInitialiazeSqlite(self):
        alc = Sqlite(config['development']['db'])

        query = "schema_info, schema_type"
        sql = RemoveIndex(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_type", sql.column_name)
        self.assertEqual("DROP INDEX schema_type;", sql.query)

    def testInitialiazeDb2(self):
        alc = Db2(config['development']['db'])

        query = "schema_info, schema_type"
        sql = RemoveIndex(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_type", sql.column_name)
        self.assertEqual("DROP INDEX schema_type;", sql.query)

    def testInitialiazeOracle(self):
        alc = Oracle(config['development']['db'])

        query = "schema_info, schema_type"
        sql = RemoveIndex(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_type", sql.column_name)
        self.assertEqual("DROP INDEX schema_type;", sql.query)

    def testInitialiazePostgres(self):
        alc = Postgres(config['development']['db'])

        query = "schema_info, schema_type"
        sql = RemoveIndex(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_type", sql.column_name)
        self.assertEqual("DROP INDEX schema_type;", sql.query)

    def testInitialiazeMssql(self):
        alc = Mssql(config['development']['db'])

        query = "schema_info, schema_type"
        sql = RemoveIndex(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_type", sql.column_name)
        self.assertEqual("DROP INDEX schema_info.schema_type;", sql.query)

    def testExecute(self):
        alc = Sqlite(config['development']['db'])
        alc._create_schema()
        alc.execute("CREATE INDEX schema_type ON schema_info (version ASC) ;")
        query = "schema_info, schema_type"
        sql = RemoveIndex(alc, query)

        self.assertTrue(sql.execute())


if __name__ == "__main__":
    unittest.main()